package com.training.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.training.model.City;
import com.training.service.impl.CityServiceImpl;

@RestController
@RequestMapping(value="/api/city")
public class CityController {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private DiscoveryClient discoveryClient;
	
	@Autowired
	private CityServiceImpl cityService;

	@RequestMapping(value = "/detail" ,method = RequestMethod.GET)
    public City detail(@RequestParam Integer id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        City city = cityService.selectById(id);
        logger.info("/detail, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + city.toString());
        return city;
    }
    
    /**
     * 本地服务实例的信息
     * @return
     */
    @RequestMapping(value = "/info" ,method = RequestMethod.GET)
    public ServiceInstance showInfo() {
      ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
      return localServiceInstance;
    }
}
